package com.example.yichuangyun.mvvm.ui.activity.order.view;

import androidx.lifecycle.MutableLiveData;

import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.model.order.OrderListDataModel;
import com.example.yichuangyun.model.order.OrderListParamsModel;
import com.example.yichuangyun.mvvm.base.BaseViewModel;
import com.example.yichuangyun.mvvm.di.retrofit.BaseModel;
import com.example.yichuangyun.mvvm.di.retrofit.BaseNetCallback;
import com.example.yichuangyun.utils.UMengUtil;
import com.example.yichuangyun.utils.treelist.SetShareBean;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.subscribers.ResourceSubscriber;

public class OrderListViewModel extends BaseViewModel {

    public MutableLiveData<OrderListDataModel> data = new MutableLiveData<>();
    public final MutableLiveData<SetShareBean> treeData = new MutableLiveData<>();
    public final MutableLiveData<String> confirmSuccessData = new MutableLiveData<>();

    public void getOrderList(OrderListParamsModel orderListParamsModel) {
        addSubscribe(mRetrofitHelper.getOrderList(orderListParamsModel).subscribeWith(new BaseNetCallback<OrderListDataModel>() {

            @Override
            public void onSuccess(BaseModel<OrderListDataModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    data.setValue(baseModel.getData());
                } else {
                    mErrorData.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                mErrorData.setValue(message);
            }
        }));
    }

    public void getTreeList(String keyword) {
        JSONObject requestData = new JSONObject();
        try {
            requestData.put("keyword", keyword);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        addSubscribe(mRetrofitHelper.getTreeData(requestData).subscribeWith(new ResourceSubscriber<SetShareBean>() {

            @Override
            public void onNext(SetShareBean setShareBean) {
                if (Constant.SUCCESS == setShareBean.getResult()) {
                    treeData.setValue(setShareBean);
                }
            }

            @Override
            public void onError(Throwable t) {
                mErrorData.setValue(t.getMessage());
            }

            @Override
            public void onComplete() {

            }
        }));
    }

    /**
     * 款项确认
     *
     * @param id 订单id
     */
    public void moneyConfirm(String id) {
        UMengUtil.setCountEventId(Constant.ORDER_MONEY_CONFIRM);
        List<String> ids = new ArrayList<>();
        ids.add(id);
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        addSubscribe(mRetrofitHelper.moneyConfirm(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<String>() {

            @Override
            public void onSuccess(BaseModel<String> baseModel) {
                if (0 == baseModel.getCode()) {
                    confirmSuccessData.setValue(baseModel.getMsg());
                } else {
                    mErrorData.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                mErrorData.setValue(message);
            }
        }));
    }
}
